package com.qf.team2.netdisc.service.impl;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.entity.Link;
import com.qf.team2.netdisc.common.pojo.qo.LinksDetails;
import com.qf.team2.netdisc.common.pojo.qo.ShareLinkQO;
import com.qf.team2.netdisc.common.util.ComputationTimeUtils;
import com.qf.team2.netdisc.dao.LinkMapper;
import com.qf.team2.netdisc.entity.Code;
import com.qf.team2.netdisc.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件分享服务接口实现类
 *
 * @author Simon_Wong
 * @date 2021/05/26 11:38
 */
@Service
public class ShareServiceImpl implements ShareService {
    @Autowired
    private LinkMapper linkMapper;

    /**
     * 将shareLinkQO的属性封装到 link类
     * 同时创建链接和提取码。并且存储到数据库中
     * @param shareLinkQO：前端传递的对象数据，详情{@link ShareLinkQO}中查看
     * @return link
     */
    @Override
    public Link createLink(ShareLinkQO shareLinkQO) {
        Link link = new Link();
        Code cod = new Code();
        System.out.println(shareLinkQO);
        link.setUrl("http://localhost:8080/netdisc/views/view-api/checkCode.html?" + cod.creatUrl());
        Document document = linkMapper.selectDocumentById(Integer.valueOf(shareLinkQO.getFileIdVal()));
        System.out.println(link);
        System.out.println(document);
        link.setMd5(document.getMd5());
        link.setUserId(Integer.valueOf(shareLinkQO.getUserIdVal()));
        link.setDocumentId(Integer.valueOf(shareLinkQO.getFileIdVal()));
        if ("".equals(shareLinkQO.getCodeVal())) {
            String systemCode = cod.creatCode();
            link.setVerifyCode(systemCode);
        } else {
            link.setVerifyCode(shareLinkQO.getCodeVal());
        }
        if ("forever".equals(shareLinkQO.getAgingVal())) {
            link.setDeadline(0L);
        } else {
            link.setDeadline(Long.parseLong(shareLinkQO.getAgingVal()) * 86400000);
        }
        link.setPrivate(false);
        linkMapper.insertLink(link);
        return link;
    }

    @Override
    public void test() {
        System.out.println("test");
    }

    @Override
    public Integer updateIsPrivateStatus(Integer linkId) {
        return linkMapper.updateIsPrivate(linkId);
    }

    @Override
    public List<Link> selectAllLink() {
        return linkMapper.selectAllLink();
    }

    /**
     * linksDetails中的Deadline属性由毫秒转化为天数，
     * 并且存储到newLinksDetails中返回到ShareController
     * @param userId  用户ID
     * LinksDetails：整合数据库LINK  以及数据库DOC中的数据并存储到LinksDetails中
     * @return
     */
    @Override
    public List<LinksDetails> selectLinksDetails(int userId) {
        List<LinksDetails> linksDetails = linkMapper.selectLinksDetails(userId);
        List<LinksDetails> newLinksDetails=new ArrayList<>();
        for (LinksDetails ld:linksDetails) {
            if (ld.getDeadline() != 0) {
                long result = ComputationTimeUtils.computationTime(ld.getCreateTime());
                long deadline = ld.getDeadline();
                long day = (deadline - result) / 86400000;
                System.out.println(day);
                ld.setDeadline(day);
                newLinksDetails.add(ld);
            }else{
                //此处暂时把永久分享的链接时效设置为9999，前端识别时效为9999的链接时自动渲染成永久有效
                ld.setDeadline(9999);
                newLinksDetails.add(ld);
            }
        }
        return newLinksDetails;
    }
}
